Page({
  data: {
    checkinList: [],
    loading: false
  },
  onLoad() {
    this.loadAllCheckin();
  },
  async loadAllCheckin() {
    this.setData({ loading: true });
    wx.request({
      url: 'http://localhost:1002/tea/checkin/list',
      method: 'GET',
      success: (res) => {
        console.log('全部打卡原始数据', res.data);
        // 格式化时间字段（适配驼峰命名）
        const list = (res.data || []).map(item => ({
          ...item,
          startTime: item.startTime ? formatDate(item.startTime) : '',
          endTime: item.endTime ? formatDate(item.endTime) : '',
          createTime: item.createTime ? formatDate(item.createTime) : ''
        }));
        this.setData({ checkinList: list, loading: false });
      },
      fail: () => {
        wx.showToast({ title: '加载失败', icon: 'none' });
        this.setData({ loading: false });
      }
    });
  }
});

function formatDate(dateStr) {
  if (!dateStr) return '';
  let d;
  if (typeof dateStr === 'number') {
    d = new Date(dateStr);
  } else {
    d = new Date(dateStr); // 直接解析ISO格式
  }
  if (isNaN(d.getTime())) return '';
  const y = d.getFullYear();
  const m = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');
  const h = d.getHours().toString().padStart(2, '0');
  const min = d.getMinutes().toString().padStart(2, '0');
  return `${y}-${m}-${day} ${h}:${min}`;
}

